package com.fly.controller;

import com.fly.domain.Menu;
import com.fly.service.MenuService;
import com.fly.vo.Response;
import com.fly.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Fly
 * @Description
 * @Date Created in 21:50 2018/1/30
 * @Modified by
 */
@RequestMapping("menus")
@RestController
public class MenuController extends BaseController {

    private final MenuService menuService;

    @Autowired
    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping("/current")
    public List<Menu> getCurrentMenus(){
        return menuService.getCurrentMenus();
    }

    /**
     * 获取菜单列表
     *
     * @return
     */
    @GetMapping()
    public List<Menu> list() {
        return menuService.findAll();
    }

    /**
     * 保存
     *
     * @param menu
     * @return
     */
    @PostMapping()
    public ResponseEntity<Response> save(@RequestBody Menu menu) {
        Menu rMenu = menuService.save(menu);
        return ResponseEntity.ok(new Response(true, "保存成功", rMenu));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Response> remove(@PathVariable("id") Long id) {
        menuService.delete(id);
        return ResponseEntity.ok(new Response(true, "删除成功", null));
    }


    /**
     * 根据parentID获取树形数据
     * @param roleId
     * @return
     */
    @GetMapping("tree/{roleId}")
    public Map<String,Object> getByRoleId(@PathVariable("roleId")Long roleId){
        Map<String,Object> map = new ConcurrentHashMap<>();
        List<Long> menuIds =  menuService.getMenusByRoleId(roleId);
        List<Menu> menuList = menuService.getTreeData(Long.valueOf(-1));
        map.put("menuIds",menuIds);
        map.put("menus",menuList);
        return map;
    }

    /**
     * 根据parentID获取树形数据
     * @param parentId
     * @return
     */
/*    @GetMapping("tree/{parentId}")
    public List<Menu> getByParentId(@PathVariable("parentId")Long parentId){
        return menuService.getTreeData(parentId);
    }*/
}
